<template>
  <div>
    <h3>区域管理表格</h3>
    <el-input
        placeholder="请输入区域名称"
        v-model="searchQuery"
        style="width: 200px; margin-right: 10px;"
        clearable
        @clear="fetchAreas"
    />
    <el-button type="primary" @click="searchAreas">搜索</el-button>
    <el-button type="primary" @click="openDialog">添加区域</el-button>
    <el-table :data="areas" style="width: 100%; margin-top: 20px;">
      <!-- 表格列定义 -->
      <el-table-column prop="name" label="区域名称" width="180"></el-table-column>
      <el-table-column prop="province" label="省份" width="180"></el-table-column>
      <el-table-column prop="createTime" label="创建时间" width="180"></el-table-column>
      <el-table-column label="操作" width="180">
        <template v-slot="scope">
          <el-button size="small" @click="editArea(scope.row)">编辑</el-button>
          <el-button size="small" type="danger" @click="deleteArea(scope.row.name)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog :title="isEdit ? '编辑区域' : '添加区域'" v-model="dialogVisible">
      <el-form :model="newArea" :rules="rules" ref="areaForm">
        <!-- 表单项定义 -->
        <el-form-item label="区域名称" :label-width="formLabelWidth" prop="name">
          <el-input v-model="newArea.name"></el-input>
        </el-form-item>
        <el-form-item label="省份" :label-width="formLabelWidth" prop="province">
          <el-input v-model="newArea.province"></el-input>
        </el-form-item>
        <el-form-item label="创建时间" :label-width="formLabelWidth" prop="createTime">
          <el-date-picker v-model="newArea.createTime" type="date" placeholder="选择创建时间"></el-date-picker>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="addOrUpdateArea">确定</el-button>
        <el-button @click="dialogVisible = false">取消</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import axios from 'axios';
import { ElMessage } from 'element-plus';

export default {
  data() {
    return {
      areas: [],
      dialogVisible: false,
      isEdit: false,
      searchQuery: '',
      newArea: {
        name: '',
        province: '',
        createTime: ''
      },
      formLabelWidth: '120px',
      rules: {
        name: [{ required: true, message: '请输入区域名称', trigger: 'blur' }],
        province: [{ required: true, message: '请输入省份', trigger: 'blur' }],
        createTime: [{ required: true, message: '请选择创建时间', trigger: 'change' }]
      }
    };
  },
  created() {
    this.fetchAreas();
  },
  methods: {
    fetchAreas() {
      axios.get('http://localhost:8081/api/area/getall')
          .then(response => {
            this.areas = response.data;
          })
          .catch(error => {
            console.error("There was an error fetching the areas!", error);
          });
    },
    searchAreas() {
      axios.get(`http://localhost:8081/api/area/get/${this.searchQuery}`)
          .then(response => {
            this.areas = [response.data];
          })
          .catch(error => {
            console.error("There was an error searching the area!", error);
          });
    },
    openDialog() {
      this.isEdit = false;
      this.dialogVisible = true;
      this.resetArea();
    },
    editArea(area) {
      this.isEdit = true;
      this.dialogVisible = true;
      this.newArea = { ...area };
    },
    addOrUpdateArea() {
      this.$refs.areaForm.validate(valid => {
        if (valid) {
          if (this.isEdit) {
            axios.put('http://localhost:8081/api/area/update', this.newArea)
                .then(() => {
                  this.dialogVisible = false;
                  this.fetchAreas();
                  this.resetArea();
                  ElMessage.success('区域信息更新成功');
                })
                .catch(error => {
                  console.error("There was an error updating the area!", error);
                  ElMessage.error('区域信息更新失败');
                });
          } else {
            axios.post('http://localhost:8081/api/area/add', this.newArea)
                .then(() => {
                  this.dialogVisible = false;
                  this.fetchAreas();
                  this.resetArea();
                  ElMessage.success('区域信息添加成功');
                })
                .catch(error => {
                  console.error("There was an error adding the area!", error);
                  ElMessage.error('区域信息添加失败');
                });
          }
        }
      });
    },
    deleteArea(name) {
      axios.delete(`http://localhost:8081/api/area/delete/${name}`)
          .then(() => {
            this.fetchAreas();
            ElMessage.success('区域信息删除成功');
          })
          .catch(error => {
            console.error("There was an error deleting the area!", error);
            ElMessage.error('区域信息删除失败');
          });
    },
    resetArea() {
      this.newArea = {
        name: '',
        province: '',
        createTime: ''
      };
    }
  }
};
</script>

<style scoped>
.dialog-footer {
  text-align: right;
}
</style>
